package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.module.admin.entity.Award;
import com.mars.module.admin.mapper.AwardMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.ActivityRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.ActivityMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.Activity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IActivityService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 活动业务层处理
 *
 * @author mars
 * @date 2024-04-23
 */
@Slf4j
@Service
@AllArgsConstructor
public class ActivityServiceImpl implements IActivityService {

    private final ActivityMapper baseMapper;
    private final AwardMapper awardMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Activity add(ActivityRequest request) {
        Activity entity = Activity.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ActivityRequest request) {
        Activity entity = Activity.builder().build();
        BeanUtils.copyProperties(request, entity);
        String activityName = entity.getActivityName();
        List<Award> awards = awardMapper.selectByActivityId(entity.getId());
        for (Award award : awards) {
            award.setName(activityName);
            awardMapper.updateById(award);
        }
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public Activity getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<Activity> pageList(ActivityRequest request) {
        Page<Activity> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<Activity> query = this.buildWrapper(request);
        IPage<Activity> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<Activity> list(ActivityRequest request) {
        LambdaQueryWrapper<Activity> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<Activity> buildWrapper(ActivityRequest param) {
        LambdaQueryWrapper<Activity> query = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(param.getActivityName())){
               query.like(Activity::getActivityName ,param.getActivityName());
        }
        return query;
    }

}
